package com.itheima.redbaby.engine.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.GloableParams;
import com.itheima.redbaby.bean.CartItem;
import com.itheima.redbaby.bean.CartProduct;
import com.itheima.redbaby.bean.CartProductScore;
import com.itheima.redbaby.dao.CartDao;
import com.itheima.redbaby.dao.domain.CartTable;
import com.itheima.redbaby.engine.BaseEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;

public class CartEngineImpl extends BaseEngine {
	private int totalCount;
	private float totalPrice;
	private float totalPoint;
	public List<CartProductScore> getCartJson(String product_id){
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ids", product_id);
		JSONObject json = getJson(TYPE_POST,ContantValue.CART_SCORE_NUM,map);
		
		if(json.getString("response").equals("cart")){
			String cartscorelist = json.getString("cartscorelist");
			List<CartProductScore> parseArray = JSON.parseArray(cartscorelist, CartProductScore.class);
			return parseArray;
		}
		return null;
	}
	public void getCartJson(Object object,int num){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("product_id", object);
		map.put("num", num);
		JSONObject cartJson = getJson(TYPE_POST,ContantValue.CART_DETAIL,map);
		if(cartJson.getString("response").equals("cart")){
			JSONObject cartMapJson = cartJson.getJSONObject("cart");
			String cartitemlistJson=cartMapJson.getString("cartitemlist");
			//每次选货物进入购物车后数量增加
			List<CartItem> list = JSON.parseArray(cartitemlistJson, CartItem.class);
			GloableParams.cartitemlist.add(list.get(0));
			
			//根据购物车内的商品数量计算出总的价钱和数量
			if(GloableParams.cartMap.size()!=0){
				totalCount = cartMapJson.getIntValue("totalCount")+ (Integer) GloableParams.cartMap.get("totalCount");
				totalPrice = cartMapJson.getFloatValue("totalPrice")+ (Float) GloableParams.cartMap.get("totalPrice");
				totalPoint = cartMapJson.getFloatValue("totalPoint")+ (Float) GloableParams.cartMap.get("totalPoint");
				
			}else{
				totalCount = cartMapJson.getIntValue("totalCount");
				totalPrice = cartMapJson.getFloatValue("totalPrice");
				totalPoint = cartMapJson.getFloatValue("totalPoint");
			}
			
			
			GloableParams.cartMap.put("cartitemlist", GloableParams.cartitemlist);
			GloableParams.cartMap.put("totalCount", totalCount);
			GloableParams.cartMap.put("totalPrice", totalPrice);
			GloableParams.cartMap.put("totalPoint", totalPoint);
			
			//购物车数据实例化本地数据库
			CartDao nativeCartDao = DaoAndEngineFactory.getImpl(CartDao.class);
			nativeCartDao.delete();
			//CartDaoImpl nativeCartDao=new CartDaoImpl();
			for(int i=0;i<GloableParams.cartitemlist.size();i++){
				CartTable cartTable=new CartTable();
				CartItem cartItem = GloableParams.cartitemlist.get(i);
				CartProduct product = cartItem.getProduct();
				int productprom = cartItem.getProductprom();
				
				cartTable.setProduct_id(product.getProduct_id());
				cartTable.setName(product.getName());
				cartTable.setPicpath(product.getPicpath());
				cartTable.setPrice(product.getPrice());
				cartTable.setScore(product.getScore());
				cartTable.setBuylimit(product.getBuylimit());
				cartTable.setIsgift(product.getIsgift());
				cartTable.setProductprom(productprom);
				
				nativeCartDao.insert(cartTable);
			}
		}else{
			System.out.println("网络异常");
		}
	}
}
